﻿
Partial Class Article_List_Right_Mode1
    Inherits System.Web.UI.Page
    Dim CArray(40, 13)
    Public Shared Function MD5(ByVal strSource As String, ByVal Code As Int16) As String
        '这里用的是ascii编码密码原文，如果要用汉字做密码，可以用UnicodeEncoding，但会与ASP中的MD5函数不兼容 
        Dim dataToHash As Byte() = (New System.Text.ASCIIEncoding).GetBytes(strSource)
        Dim hashvalue As Byte() = CType(System.Security.Cryptography.CryptoConfig.CreateFromName("MD5"), System.Security.Cryptography.HashAlgorithm).ComputeHash(dataToHash)
        Dim i As Integer
        Select Case Code
            Case 16 '´选择16位字符的加密结果 
                For i = 4 To 11
                    MD5 += Hex(hashvalue(i)).ToLower
                Next
            Case 32 ' ´选择32位字符的加密结果 
                For i = 0 To 15
                    MD5 += Hex(hashvalue(i)).ToLower
                Next
            Case Else ' ´Code错误时，返回全部字符串，即32位字符 
                For i = 0 To hashvalue.Length - 1
                    MD5 += Hex(hashvalue(i)).ToLower
                Next
        End Select
    End Function
    Function EncodeBase64(ByVal StrA)
        Dim BufferA As Byte()
        BufferA = System.Text.Encoding.Default.GetBytes(StrA)
        Dim StrB As String
        StrB = Convert.ToBase64String(BufferA)
        EncodeBase64 = StrB
    End Function
    Function DecodeBase64(ByVal StrA)
        DecodeBase64 = Encoding.GetEncoding("gb2312").GetString(Convert.FromBase64String(StrA))
    End Function
    Function getstr(ByVal iRemote)
        Dim xPost, sGet
        iRemote = iRemote
        xPost = SERVER.CreateObject("msxml2.serverxmlhttp") 'msxml2.serverxmlhttp
        xPost.Open("GET", iRemote, False)
        xPost.setrequestheader("referer", "http://taoke.alimama.com/spreader/search_auction.htm")
        xPost.Send()
        sGet = CreateObject("ADODB.Stream")
        sGet.Mode = 3
        sGet.Type = 1
        sGet.Open()
        On Error Resume Next

        sGet.Write(xPost.responseBody)
        sGet.Position = 0
        sGet.Type = 2
        sGet.Charset = "gb2312" ' "gb2312"
        getstr = sGet.ReadText
    End Function

    Function gethtml(ByVal tmp, ByVal s, ByVal e, ByVal id)
        'On Error Resume Next
        Dim String1, String2, arrstr, j
        String1 = s
        String2 = e
        Dim fbegin
        Dim fend
        fbegin = 1
        fend = 1
        j = 0
        Do
            fbegin = InStr(fbegin, tmp, String1)
            If fbegin = 0 Then Exit Function
            fend = InStr(fbegin, tmp, String2)
            arrstr = Mid(tmp, fbegin + Len(String1), fend - (fbegin + Len(String1)))
            If id <> -1 Then
                CArray(j, id) = arrstr
                j = j + 1
            End If
            gethtml = arrstr

            fbegin = fend + Len(String2)
        Loop While True
    End Function


    Function LoadFile(ByVal File)
        Dim objStream
        On Error Resume Next
        objStream = Server.CreateObject("ADODB.Stream")
        If Err.Number = -2147221005 Then
            Err.Clear()
            Response.End()
        End If
        With objStream
            .Type = 2
            .Mode = 3
            .Open()
            .LoadFromFile(Server.MapPath(File))
            If Err.Number <> 0 Then
                Err.Clear()
                Response.End()
            End If
            .Charset = "GB2312"
            .Position = 2
            LoadFile = .ReadText
            .Close()
        End With
        objStream = Nothing
    End Function



    Public Shared Function MD5x(ByVal strSource As String, ByVal Code As Int16) As String
        '这里用的是ascii编码密码原文，如果要用汉字做密码，可以用UnicodeEncoding，但会与ASP中的MD5函数不兼容 
        Select Case Code
            Case 16 '´选择16位字符的加密结果 
                MD5x = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strSource, "MD5").ToLower().Substring(8, 16)
            Case 32 ' ´选择32位字符的加密结果 
                MD5x = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strSource, "MD5").ToLower()
        End Select
    End Function
    Protected Sub GoodsRobot_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' response.write(DecodeBase64("aHR0cDovL3N1cHBvcnQudGFvZGQub3JnL3Blcm1pc3Npb24uYXNweA==") & "?key=" & EncodeBase64(Request.ServerVariables("server_name") & "/_\" & DateTime.Today))
        ' response.end()
        Dim x, b, n, d, j, f, g, r, ur, i
        If Request.Cookies("TaoddAdmin") Is Nothing Or Request.Cookies("TaoddPassWord") Is Nothing Then
            Response.Redirect("login.aspx")
        Else
            PassPortCheck()
        End If
        Dim c


        If Request("Action") = "Get" Then
            Dim url, tempcode, cid, a, CC, pagescount, curpage, h, Count, taokeurl, rs, conn, connstr
            Dim ItemDescIUrl, ReviewsUrl, ItemViewsUrl, attributeslist, TaoBaoSale
            cid = Request("cid")
            If cid = "" Then cid = 16
            Randomize()
            url = "http://taoke.alimama.com/spreader/new_search_auction_from_engine.do?_input_charset=gb2312&c=0&od=" & Request("od") & "&q=" & cid & "----&mid=0&p=" & Request("page") & "&coms=" & Request("coms") & "&come=" & Request("come") & "&crs=" & Request("crs") & "&cre=" & Request("cre") & "&rs=" & Request("rs") & "&re=" & Request("re") & "&loc=" & Request("Loc") & "&cs=" & Request("cs") & "&ce=" & Request("ce") & "&hs=" & Request("hs") & "&he=" & Request("he") & "&rnd=" & Rnd()
            ' Response.Write(url)
            'Response.End()
            tempcode = getstr(url)
            a = gethtml(tempcode, """title"":""", """,""", 0)  '商品名称
            a = gethtml(tempcode, """reservePriceMoney"":""", """,""", 1)  '商品价格
            a = gethtml(tempcode, """commissionRateReal"":""", """,""", 6)  '佣金比例
            a = gethtml(tempcode, """commission"":""", """,""", 7)  '佣金
            'a= gethtml(tempcode, """totalFeeMoney"":""", """,""",1)  '总支出佣金
            a = gethtml(tempcode, """totalNum"":""", """,""", 12)  '累积推广量
            a = gethtml(tempcode, """nick"":""", """,""", 11)  '商品掌柜
            'a= gethtml(tempcode, """rateSum"":""", """,""",1)  '掌柜信誉(1心:1 5皇冠:20)
            a = gethtml(tempcode, """location"":""", """,""", 2)  '商品地址
            a = gethtml(tempcode, """category"":""", """,""", 8)  '商品分类ID
            'a= gethtml(tempcode, """shopCommissionRateReal"":""",""",""",1)  '商品佣金比例
            'a = gethtml(tempcode, """userId"":""", """,""", 11)  '商品RATE ID
            'a= gethtml(tempcode, """mmid"":""", """,""",1)  '商品会员ID(http://store.taobao.com/shop/view_shop.htm?user_number_id=ID)
            a = gethtml(tempcode, """auction_url"":""", """,""", 3)  '商品正常链接
            a = gethtml(tempcode, """auctionId"":""", """,""", 4)  '商品ID

            a = gethtml(tempcode, """pictUrl"":""", """,""", 5)  '商品PIC

            '  C = gethtml(tempcode, """nav""", """}", 10) '当前分类总数a
            '  Response.End()
            CC = gethtml(C, "totalnum"":", ",", 10) '当前分类总数b

            pagescount = gethtml(tempcode, """max"":", ",", 10) '当前分类名字
            curpage = gethtml(tempcode, """cur"":", ",""from", 10) '当前分类名字
            If pagescount = 100 Then CC = 100 * 40
            C = 0
            If Request("Count") = "-286" Then
                CC = 40
            Else
                If Request("Count") = "" Then
                    Count = 0
                Else
                    Count = Request("Count")
                End If
                CC = Int(Count)
            End If
            h = 0
            connstr = ConfigurationSettings.AppSettings("SQLConnString") & """" & Server.MapPath(".") & "\..\" & ConfigurationSettings.AppSettings("dbPath") & """"
            conn = Server.CreateObject("ADODB.Connection")
            conn.open(connstr)
            For i = 0 To 39
                If CArray(i, 1) <> "" Then
                    If C < CC Then


                        rs = Server.CreateObject("adodb.recordset")
                        rs.open("select  top 1 *  from commodity where CID='" & CArray(i, 4) & "'", conn, 1, 3)
                        If rs.eof Then
                            rs.addnew()
                            taokeurl = "http://taoke.alimama.com/spreader/gen_single_code.htm?auction_id=" & CArray(i, 4)
                            tempcode = getstr(taokeurl)
                            taokeurl = getText(tempcode, "name=""foroverflow"">", "</textarea>")
                            taokeurl = Replace(taokeurl, "_10011550_", "_Pid_")
                            tempcode = getstr(CArray(i, 3))
                            If InStr(tempcode, "apiItemDesc"":""") Then
                                ItemDescIUrl = getText(tempcode, "apiItemDesc"":""", """,")
                                rs("ItemDescIUrl").value = ItemDescIUrl
                            End If
                            If InStr(tempcode, "valReviewsUrl"":""") Then
                                ReviewsUrl = getText(tempcode, "valReviewsUrl"":""", """,")
                                rs("ReviewsUrl").value = ReviewsUrl

                            End If
                            If InStr(tempcode, "apiItemViews"": """) Then
                                ItemViewsUrl = getText(tempcode, "apiItemViews"": """, """,")
                                rs("ItemViewsUrl").value = ItemViewsUrl
                            End If

                            If InStr(tempcode, "attributes-list") Then
                                attributeslist = getText(tempcode, "<ul class=""attributes-list"">", "</ul>")
                                rs("attributeslist").value = attributeslist
                            End If
                            If InStr(tempcode, "30天售出：</span><em>") Then
                                TaoBaoSale = getText(tempcode, "30天售出：</span><em>", "</em>")
                                rs("TaoBaoSale").value = TaoBaoSale
                            End If
                            rs("Name").value = CArray(i, 0)
                            rs("CID").value = CArray(i, 4)
                            rs("TaokeUrl").value = taokeurl
                            rs("Curl").value = CArray(i, 3)
                            rs("Price").value = CArray(i, 1)
                            rs("Rate").value = CArray(i, 6) * 100
                            rs("Commission").value = CArray(i, 7)
                            rs("Seller").value = CArray(i, 11)
                            rs("Sale").value = CArray(i, 12)
                            rs("Pic").value = CArray(i, 5)
                            rs("class").value = CArray(i, 8)
                            rs.update()
                            rs.close()
                            C = C + 1
                            Exit For
                        End If
                        h = h + 1

                    End If
                End If
            Next
            If h <> 0 And C = 0 Then
                C = -999
            End If
            Response.Write(C)

            conn.close()

            'Response.Write(pagescount)
            Response.End()

        End If
    End Sub
    Function getText(ByVal tmp, ByVal s, ByVal e)
        'On Error Resume Next
        Dim String1, String2, arrstr, j
        String1 = s
        String2 = e
        Dim fbegin
        Dim fend
        fbegin = 1
        fend = 1
        Do
            fbegin = InStr(fbegin, tmp, String1)
            If fbegin = 0 Then Exit Function
            fend = InStr(fbegin, tmp, String2)
            arrstr = Mid(tmp, fbegin + Len(String1), fend - (fbegin + Len(String1)))
            getText = arrstr
            fbegin = fend + Len(String2)
        Loop While True
    End Function
    Function PassPortCheck()
        Dim connstr, connA, rsA
        connstr = ConfigurationSettings.AppSettings("SQLConnString") & """" & Server.MapPath(".") & "\..\" & ConfigurationSettings.AppSettings("dbPath") & """"
        connA = Server.CreateObject("ADODB.Connection")
        connA.open(connstr)
        Dim Admin_UserName, Admin_UserPass
        Admin_UserName = replace(Request.Cookies("TaoddAdmin").value, "'", "''")
        Admin_UserPass = replace(Request.Cookies("TaoddPassWord").value, "'", "''")
        rsA = Server.CreateObject("adodb.recordset")
        rsA.open("select * from [admin^] where Admin_UserName='" & Admin_UserName & "' and Admin_UserPass='" & Admin_UserPass & "'", connA, 1, 3)
        If rsA.eof Then
            Response.Redirect("login.aspx")
        End If
        connA.close()
        connA = Nothing
    End Function
End Class
